package com.uqbar.asteroids.scenes;

import java.util.ArrayList;
import java.util.Random;

import com.uqbar.asteroids.components.Asteroid;
import com.uqbar.asteroids.components.BigAsteroid;
import com.uqbar.asteroids.components.CircleShoot;
import com.uqbar.asteroids.components.Marcador;
import com.uqbar.asteroids.components.MediumAsteriod;
import com.uqbar.asteroids.components.Player;
import com.uqbar.asteroids.components.PlayerShip;
import com.uqbar.asteroids.components.TinyAsteroid;
import com.uqbar.asteroids.game.AsteroidsGame;
import com.uqbar.asteroids.imageComponents.AsteroidsBackground;
import com.uqbar.asteroids.imageComponents.ShipRender;
import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.appearances.Sprite;

public class AsteroidScene extends GameScene{
	private Player jugador;
	private PlayerShip shipselected;
	private ShipRender skinShip;
	private AsteroidsBackground background;
	private ArrayList<BigAsteroid> asteroidesGrandes = new ArrayList<BigAsteroid>();
	private ArrayList<MediumAsteriod> asteroidesMedianos = new ArrayList<MediumAsteriod>();
	private ArrayList<TinyAsteroid> asteroidesChicos = new ArrayList<TinyAsteroid>();
	private ArrayList<Asteroid> asteroidesActivos = new ArrayList<Asteroid>();
	private ArrayList<CircleShoot> poolDisparos = new ArrayList<CircleShoot>();
	private ArrayList<CircleShoot> disparosEnEscena = new ArrayList<CircleShoot>();
	private int maxScore = 0;
	private Marcador marcador;
	private ArrayList<Sprite> spritesShip = new ArrayList<Sprite>();
	
	public ArrayList<Sprite> getSpritesShip() {
		return spritesShip;
	}

	public void setSpritesShip(ArrayList<Sprite> spritesShip) {
		this.spritesShip = spritesShip;
	}

	public AsteroidsBackground getBackground() {
		return background;
	}

	public void setBackground(AsteroidsBackground background) {
		this.background = background;
		this.addComponent(this.getBackground());
	}

	public ArrayList<Asteroid> getAsteroidesActivos() {
		return asteroidesActivos;
	}

	public void setAsteroidesActivos(ArrayList<Asteroid> asteroidesActivos) {
		this.asteroidesActivos = asteroidesActivos;
	}

	public ShipRender getSkinShip() {
		return skinShip;
	}

	public void setSkinShip(ShipRender skinShip) {
		this.skinShip = skinShip;
		this.addComponent(skinShip);
	}
	
	public Marcador getMarcador() {
		return marcador;
	}

	public void setMarcador(Marcador marcador) {
		this.marcador = marcador;
		this.addComponent(this.getMarcador());
	}

	public int getMaxScore() {
		return maxScore;
	}

	public void setMaxScore(int maxScore) {
		this.maxScore = maxScore;
	}

	public ArrayList<CircleShoot> getPoolDisparos() {
		return poolDisparos;
	}

	public void setPoolDisparos(ArrayList<CircleShoot> poolDisparos) {
		this.poolDisparos = poolDisparos;
	}

	public ArrayList<BigAsteroid> getAsteroidesGrandes() {
		return asteroidesGrandes;
	}

	public void setAsteroidesGrandes(ArrayList<BigAsteroid> asteroidesGrandes) {
		this.asteroidesGrandes = asteroidesGrandes;
	}

	public ArrayList<MediumAsteriod> getAsteroidesMedianos() {
		return asteroidesMedianos;
	}

	public void setAsteroidesMedianos(ArrayList<MediumAsteriod> asteroideMediano) {
		this.asteroidesMedianos = asteroideMediano;
	}

	public ArrayList<TinyAsteroid> getAsteroidesChicos() {
		return asteroidesChicos;
	}

	public void setAsteroidesChicos(ArrayList<TinyAsteroid> asteroideChico) {
		this.asteroidesChicos = asteroideChico;
	}

	public PlayerShip getShipselected() {
		return shipselected;
	}

	public void setShipselected(PlayerShip shipselected) {
		this.shipselected = shipselected;
		this.addComponent(shipselected);
	}


	public ArrayList<CircleShoot> getDisparos() {
		return poolDisparos;
	}

	public void setDisparos(ArrayList<CircleShoot> disparos) {
		this.poolDisparos = disparos;
	}

	public ArrayList<CircleShoot> getDisparosEnEscena() {
		return disparosEnEscena;
	}

	public void setDisparosEnEscena(ArrayList<CircleShoot> disparosEnEscena) {
		this.disparosEnEscena = disparosEnEscena;
	}

	public Player getJugador() {
		return jugador;
	}

	public void setJugador(Player jugador) {
		this.jugador = jugador;
		this.addComponent(jugador);
	}

	public void agregarDisparo(CircleShoot shoot) {
		this.disparosEnEscena.add(shoot);
		this.addComponent(shoot);
	}
	
	public void shootToPool(CircleShoot shoot){
		this.poolDisparos.add(shoot);
		this.disparosEnEscena.remove(shoot);	
		shoot.toOutScreen();
	}
	
	public CircleShoot obtenerDisparo(){
		CircleShoot disparo = this.poolDisparos.get(this.poolDisparos.size()-1);
		this.poolDisparos.remove(disparo);
		this.disparosEnEscena.add(disparo);
		return disparo;
	}
	
	public void addBigAsteroid(BigAsteroid ba){
		this.asteroidesGrandes.add(ba);
	}
	
	public BigAsteroid getBigAsteroid(){
		BigAsteroid ret = this.asteroidesGrandes.get(0);
		this.asteroidesGrandes.remove(0);
		return ret;
	}
	
	public void addMediumAsteriod(MediumAsteriod ma){
		this.asteroidesMedianos.add(ma);
	}
	
	public MediumAsteriod getMediumAsteriod(){
		MediumAsteriod ret = this.asteroidesMedianos.get(0);
		this.asteroidesMedianos.remove(0);
		return ret;
	}
	
	public void addTinyAsteroid(TinyAsteroid ta){
		this.asteroidesChicos.add(ta);
	}
	
	public TinyAsteroid getTinyAsteriod(){
		TinyAsteroid ret = this.asteroidesChicos.get(0);
		this.asteroidesChicos.remove(0);
		return ret;
	}
	
	public void addAsteroidActivo(Asteroid ast){
		this.getAsteroidesActivos().add(ast);
	}
	
	public void addTinyAsteroidActivo(){
		TinyAsteroid tini = new TinyAsteroid(800,600);
		tini.activar();
		this.addComponent(tini);
		this.addAsteroidActivo(tini);
	}
	public void addMediumAsteroidActivo(){
		MediumAsteriod med = new MediumAsteriod(800,600);
		med.activar();
		this.addComponent(med);
		this.addAsteroidActivo(med);
	}
	public void addBigAsteroidActivo(){
		BigAsteroid big = new BigAsteroid(800,600);
		big.activar();
		this.addComponent(big);
		this.addAsteroidActivo(big);
	}
	
	
	public void agregarAsteroides(int n){
		for(int i = 0; i<n;i++){
			Random m = new Random();
			int aleatorio = m.nextInt(10);
			
			if(aleatorio >= 0 && aleatorio <= 3){
				this.setMaxScore(this.getMaxScore() + 20);
				this.addTinyAsteroidActivo();
				
			}

			if(aleatorio > 7){
				this.setMaxScore(this.getMaxScore() + 90);
				this.addMediumAsteroidActivo();
			}
			if(aleatorio > 3 && aleatorio <= 7){
				this.setMaxScore(this.getMaxScore() + 205);
				this.addBigAsteroidActivo();
			}
			
			
			
		}
		
		
	}
	
	public void sustituirMarcadorPorMarcador(Marcador marker){
		this.setMarcador(marker);
		this.addComponent(marker);
	}
	
	public void inicializarSprites(String imagePath){
		ArrayList<Sprite> sprites = new ArrayList<Sprite>();
		Sprite sprite = Sprite.fromImage(imagePath);
		double delta = 0;
		for(int i = 0; i < 100 ;i++){
			Sprite rotado = sprite.rotate(delta);
				sprites.add(rotado);
			delta = delta + 0.0628;
		}
		this.setSpritesShip(sprites);
	}



	public void fin() {

		this.getGame().setCurrentScene(((AsteroidsGame) this.getGame()).buildEndScene(this.getMarcador()));
		this.getMarcador().resetLevelValue();
	}
	
	public AsteroidScene(PlayerShip p, String imagePath){
		this.setShipselected(p);
		this.inicializarSprites(imagePath);
		this.setBackground(new AsteroidsBackground());
		
		this.setSkinShip(new ShipRender(this.getSpritesShip().get(0),this.getShipselected().getX()-60,this.getShipselected().getY() - 40));
	}
	
}
